import {Color} from "../core/color";
import { Material2 } from "./material2";
import {VertexData} from "../core/vertexData";
import {bindVAO, createVAO, deleteVAO, deleteVBO, unbindVAO} from "../core/utils";
import {Renderer2} from "../core/renderer2";

export class BasicMaterial extends Material2{

    static ProgramName = "BasicMaterial";
    constructor(
        vertexData:VertexData,
        color:Color = new Color(0xffffffff)){
        super(vertexData,color);
    }

    init(): void {
        let renderer = Renderer2.instance();
        let program = renderer.getProgame(BasicMaterial.ProgramName);
        let gl = renderer.gl;

        this.vao = createVAO(gl);
        bindVAO(gl,this.vao!);
        this.checkPosVBO(gl,program);
        this.checkIndiceVBO(gl);

        // wireframe
        let wireFrameProgram = renderer.getProgame(Material2.WireFrame);
        this.lineVAO = createVAO(gl);
        bindVAO(gl,this.lineVAO!);
        this.checkLinePosVBO(gl,wireFrameProgram);
        this.checkLineIndiceVBO(gl);

        // boundingbox
        this.bbVAO = createVAO(gl);
        bindVAO(gl,this.bbVAO!);
        this.checkBBPosVBO(gl,wireFrameProgram);
        this.checkBBIndiceVBO(gl);

        unbindVAO(gl);
    }

    cleanup(): void {
        let gl = Renderer2.instance().gl;
        deleteVAO(gl,this.vao);
        deleteVBO(gl,this.posVBO);
        deleteVBO(gl,this.indiceVBO);

        deleteVAO(gl,this.lineVAO);
        deleteVBO(gl,this.linePosVBO);
        deleteVBO(gl,this.lineIndiceVBO);

        deleteVAO(gl,this.bbVAO);
        deleteVBO(gl,this.bbPosVBO);
        deleteVBO(gl,this.bbIndicesVBO);
    }


}
